package com.ca.ridersclubapplication.view.list;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.util.ImageUtils;

import java.util.ArrayList;
import java.util.List;

public class MiniHeaderImageListView extends ConstraintLayout {

    private String[] imageUrlArray;
    private final List<ImageView> list = new ArrayList<>();

    public MiniHeaderImageListView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public MiniHeaderImageListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MiniHeaderImageListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.mini_header_image_list_layout, this);
        list.add(view.findViewById(R.id.iv_1));
        list.add(view.findViewById(R.id.iv_2));
        list.add(view.findViewById(R.id.iv_3));

        setImageUrlArray(null);
    }

    public int getSize() {
        return list.size();
    }

    public ImageView get(int index) {
        return list.get(index);
    }

    public void setImageUrlArray(String[] imageUrlArray) {
        this.imageUrlArray = imageUrlArray == null ? new String[0] : imageUrlArray;
        for (int i = 0; i < 3; i++) {
            ImageView imageView = this.list.get(i);
            if (i < this.imageUrlArray.length) {
                String url = this.imageUrlArray[i];
                imageView.setVisibility(VISIBLE);
                ImageUtils.loadCircleImage(getContext(), url, imageView);
            }
            else {
                imageView.setVisibility(GONE);
            }
        }
    }

    public String[] getImageUrlArray() {
        return imageUrlArray;
    }
}
